package com.sainthigh.baidumapdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.orhanobut.logger.Logger;

public class MainActivity extends AppCompatActivity {


    MapView mMapView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现

        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.bmapView);
        initView();
    }

    private void initView() {
        findViewById(R.id.map_btn_location).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final GeoCoder geoCoder = GeoCoder.newInstance();
                geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
                    @Override
                    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                        if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                            //没有检索到结果
                        }
                        //获取地理编码结果
                        Logger.t("MainActivity").d("onGetGeoCodeResult");
                        Logger.t("MainActivity").d(geoCodeResult);
                        Logger.t("MainActivity").d(geoCodeResult.getAddress());
                        Logger.t("MainActivity").d(geoCodeResult.getLocation().latitude + " , " + geoCodeResult.getLocation().latitude);
                        geoCoder.destroy();
                    }

                    @Override
                    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                        if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                            //没有找到检索结果
                        }
                        //获取反向地理编码结果
                        Logger.t("MainActivity").d("onGetReverseGeoCodeResult");
                        geoCoder.destroy();
                    }
                });
                geoCoder.geocode(new GeoCodeOption().city("北京").address("海淀区上地十街10号"));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
}

